﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media.Imaging;
using VM.Start.Core;
using VM.Start.Models;
using VM.Start.Services;

namespace VM.Start.Common
{
    public class CommonMethods
    {
        public static MachineModel Mach = new MachineModel();
        #region UI线程操作
        /// <summary>
        /// UI同步操作
        /// </summary>
        /// <param name="action"></param>
        public static void UISync(Action action)
        {
            System.Windows.Application.Current.Dispatcher.Invoke(() => action?.Invoke());
        }

        /// <summary>
        /// UI异步操作
        /// </summary>
        /// <param name="action"></param>
        public static void UIAsync(Action action)
        {
            System.Windows.Application.Current.Dispatcher.BeginInvoke(action);
        }
        #endregion

        #region  方法

        public static void GetModuleList(
            ModuleParam moduleParam,
            ObservableCollection<ModuleList> Modules,
            string dataType = "")
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                if (Modules == null)
                    Modules = new ObservableCollection<ModuleList>();
                string[] types = dataType.Split(',');
                List<VarModel> SysVar = new List<VarModel>();
                var pro = Solution.Ins.GetProjectById(moduleParam.ProjectID);
                int index = pro.GetModuleIndexByName(moduleParam.ModuleName);
                List<VarModel>[] ModuleVars = new List<VarModel>[index];
                for (int i = 0; i < index; i++)
                {
                    ModuleVars[i] = new List<VarModel>();
                }
                Modules.Clear();

                if (dataType == "")
                {
                    SysVar = Solution.Ins.SysVar
                        .Select(
                            o =>
                                new VarModel()
                                {
                                    ModuleParam = o.ModuleParam,
                                    Name = o.Name,
                                    DataType = o.DataType,
                                    Text = o.Text,
                                    Value = o.Value,
                                    Note = o.Note
                                }
                        )
                        .ToList();
                    for (int j = 0; j < index; j++)
                    {
                        if (pro.OutputMap.ContainsKey(pro.ModuleList[j].ModuleParam.ModuleName))
                        {
                            ModuleVars[j].AddRange(
                                pro.OutputMap[pro.ModuleList[j].ModuleParam.ModuleName].Values
                                    .Select(
                                        o =>
                                            new VarModel()
                                            {
                                                ModuleParam = o.ModuleParam,
                                                Name = o.Name,
                                                DataType = o.DataType,
                                                Value = o.Value,
                                                Note = o.Note,
                                                Text = o.Text
                                            }
                                    )
                                    .ToList()
                            );
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < types.Length; i++)
                    {
                        SysVar.AddRange(
                            Solution.Ins.SysVar
                                .Where(o => o.DataType == types[i])
                                .Select(
                                    o =>
                                        new VarModel()
                                        {
                                            Name = o.Name,
                                            DataType = o.DataType,
                                            Value = o.Value,
                                            Note = o.Note
                                        }
                                )
                                .ToList()
                        );
                        for (int j = 0; j < index; j++)
                        {
                            if (pro.OutputMap.ContainsKey(pro.ModuleList[j].ModuleParam.ModuleName))
                            {
                                ModuleVars[j].AddRange(
                                    pro.OutputMap[pro.ModuleList[j].ModuleParam.ModuleName].Values
                                        .Where(o => o.DataType == types[i])
                                        .Select(
                                            o =>
                                                new VarModel()
                                                {
                                                    ModuleParam = o.ModuleParam,
                                                    Name = o.Name,
                                                    DataType = o.DataType,
                                                    Value = o.Value,
                                                    Note = o.Note
                                                }
                                        )
                                        .ToList()
                                );
                            }
                        }
                    }
                }

                if (SysVar.Count > 0)
                {
                    Modules.Add(
                        new ModuleList()
                        {
                            VarModels = SysVar,
                            ModuleNo = 0,
                            DisplayName = "全局变量",
                            IconImage = new BitmapImage(
                                new Uri(
                                    $"/VM.Start;component/Assets/Images/Tool/GlobalVar.png",
                                    UriKind.Relative
                                )
                            ),
                            Remarks = "全局变量"
                        }
                    );
                }
                for (int i = 0; i < ModuleVars.Length; i++)
                {
                    if (ModuleVars[i].Count > 0)
                    {
                        ModuleBase moduleObj = Solution.Ins.CurrentProject.GetModuleByName(
                            ModuleVars[i][0].ModuleParam.ModuleName
                        );
                        if (moduleObj == null)
                            return;

                        //屏蔽掉“否则”模块和“结束”模块
                        if (
                            (
                                !moduleObj.ModuleParam.ModuleName.Contains("否则")
                                && !moduleObj.ModuleParam.ModuleName.Contains("结束")
                            ) || moduleObj.ModuleParam.ModuleName.Contains("否则如果")
                        )
                        {
                            Modules.Add(
                                new ModuleList()
                                {
                                    VarModels = ModuleVars[i],
                                    ModuleNo = Modules.Count,
                                    DisplayName = ModuleVars[i][0].ModuleParam.ModuleName,
                                    IconImage = ModuleNode.GetImageByName(ModuleVars[i][0].ModuleParam),
                                    Remarks = moduleObj.ModuleParam.Remarks
                                }
                            );
                        }
                    }
                }
            });
        }

        #endregion
    }
}
